<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <!-- 
        在vue2里面有几种情况会使响应式无效
          1. 在对象里面挂载新属性
          2. 直接修改数组长度
          3. 通过索引修改数组项

        用的响应式的原理用到的Object.defineProperty(),所以没有办法监听到一些数组和对象的改变
        所以vue3才改成Proxy的方法来做响应式

        可以使用浅拷贝或者$forceUpdate()方法来更新
       -->

      <h2>vue2的深层响应式</h2>
      <p>count: {{ count }}</p>
      <button v-on:click="count++">btn</button>
      <p>arr: {{ arr }}</p>
      <button v-on:click="arr.push(5)">btn2</button>
      <p>obj: {{ obj }}</p>
      <button v-on:click="fn">btn3</button>
      <p>arr2: {{ arr2 }}</p>
      <button v-on:click="fn2">btn4</button>
      <p>arr3: {{ arr3 }}</p>
      <button v-on:click="fn3">btn4</button>
    </div>

    <script src="./lib/vue2.js"></script>
    <script>
      new Vue({
        data() {
          return {
            count: 1,
            arr: [1, 2, 3],

            obj: {
              name: "xiaohua",
            },
            arr2: ["one", "two", "three"],
            arr3: ["red", "green", "blue"],
          };
        },
        methods: {
          fn() {
            this.obj.age = 20;
            // this.obj = { ...this.obj };
            this.$forceUpdate();
            console.log(this.obj);
          },
          fn2() {
            this.arr2.length = 2;
            this.arr2 = [...this.arr2];
            console.log(this.arr2);
          },
          fn3() {
            this.arr3[1] = "hi";
            this.arr3 = [...this.arr3];
            console.log(this.arr3);
          },
        },
      }).$mount("#app");
    </script>
  </body>
</html>
